package com.example.reception.controller;

import com.example.reception.controller.dto.CartDTO;
import com.example.reception.entity.Products;
import com.example.system.config.UploadFile;
import com.example.system.model.ApiResponse;
import com.example.reception.service.ProductsService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @author: adnaan
 * @create: 2023-10-20 10:04
 * @Description:
 */

@RestController
@RequestMapping("/api")
public class ProductsController {
    @Autowired
    private ProductsService ProductsService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UploadFile uploadFile;

    @RequestMapping("/commlist")
    public ApiResponse findByType(@RequestParam String productId) {
        return ProductsService.findByType(productId);
    }

    @GetMapping("/page")
    public ApiResponse findPage(@RequestParam Integer currentPage,
                                @RequestParam Integer pageSize,
                                @RequestParam String productName
    ) {
        return ProductsService.findPaginated(currentPage, pageSize, productName);
    }

    @GetMapping("/detail")
    public ApiResponse getDetail(String key) throws JsonProcessingException {
        HashOperations hashOperations = stringRedisTemplate.opsForHash();
        Object v = hashOperations.entries(key);
        // redis中没有数据, 从mongoDb中获取
        if (((Map<?, ?>) v).isEmpty()) {
            Products pro = ProductsService.findDetail(key);
            ObjectMapper mapper = new ObjectMapper();
            v = mapper.writeValueAsString(pro);

            // v 不为空的情况下，写入redis
            if (pro != null) {
                hashOperations.put(key, "name", pro.getName());
                hashOperations.put(key, "id", pro.getId());
                hashOperations.put(key, "price", Double.toString(pro.getPrice()));
                hashOperations.put(key, "image", String.format(pro.getImage()));
                hashOperations.put(key, "description", String.format(pro.getDescription()));
                /*hashOperations.putAll(key,(Map<?, ?>) v);*/
            } else {
                return null;
            }
            return ApiResponse.success(pro);
        } else {
            return ApiResponse.success(v);
        }
    }

    @PostMapping("/addProduct")
    public ApiResponse addProduct(@RequestBody Products products) {
        return ProductsService.addProduct(products);
    }

    @DeleteMapping("/delProduct")
    public ApiResponse delProduct(@RequestParam String productId) {
        return ProductsService.delProduct(productId);
    }

    @PutMapping("/updateProduct")
    public ApiResponse updateProduct(@RequestBody Products products) {
        return ProductsService.updateProduct(products);
    }

    @PostMapping("/uploadImage")
    public String uploadAvatar(@RequestParam MultipartFile file, @RequestParam String filetype) throws IOException {
        return uploadFile.upload(file,filetype);
    }

    @PostMapping("/saveCart")
    public ApiResponse addCart(@RequestBody CartDTO cartDTO) {
        return ProductsService.saveCart(cartDTO.getUsername(), cartDTO.getProductId(),cartDTO.getQuantity());
    }

    @DeleteMapping("/delCart")
    public ApiResponse delCart(@RequestBody CartDTO cartDTO) {
        return ProductsService.delCart(cartDTO.getUsername(),cartDTO.getProductId());
    }

    @GetMapping("/getCartContents")
    public ApiResponse getCartContents(@RequestParam String username) {
        return ProductsService.getCartContents(username);
    }

    @GetMapping("/hotProducts")
    public ApiResponse hotProducts(){
        return ProductsService.hotProducts();
    }
}
